import time
from datetime import datetime
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.user_dashboard_locator import UserDashboardLactor


class UserDashboardPage(Basepage):
    def view_dashboard_lastestinfo(self):
        doc='查看dashboard的lastest info栏 消息最多8条'
        time.sleep(3)
        #self.move_mouse_to_element(UserDashboardLactor.lastestInfo_title)
        notify_times = []
        eles = self.get_elements(UserDashboardLactor.lastestInfo_time_ele, doc)
        # 获取订单创建时间数组
        for ele in eles:
            notify_times.append(ele.text)
        print(notify_times)
        ##检查消息最多8条
        if 0 <= len(notify_times) <= 8:
            print("消息数量在 0 到 8 之间")
            check_info = 'YES'
        else:
            check_info = 'NO'
            print("消息数量不在 0 到 8 之间")
        assert check_info == "YES"
        # 将时间字符串转换为datetime对象，并按照时间顺序排序
        list_times = sorted(map(lambda time_str: datetime.strptime(time_str, "%d-%m-%Y %H:%M:%S"), notify_times), reverse=True)
        print(list_times)
        # 将datetime对象列表转换回字符串列表，并按照"%d-%m-%Y %H:%M:%S"格式进行格式化
        dates_as_str = [date.strftime("%d-%m-%Y %H:%M:%S") for date in list_times]
        print(dates_as_str)
        # 检查排序后的时间列表是否与原始时间列表相同
        if dates_as_str == notify_times:
            print("时间按倒序排列")
            result = True
        else:
            print("时间列表未按倒序排列")
            result = False
    #####备注是未读的展示在最前面，后才是按照时间排序

    def more_to_allNotify(self):
        doc='从dashboard的lastest info点击more按钮进入all notify页面'
        self.wait_elevisble(UserDashboardLactor.lastestInfo_more_ele, doc)
        self.click_element(UserDashboardLactor.lastestInfo_more_ele, doc)
        self.wait_elevisble(UserDashboardLactor.all_noti_title, doc)
        notify_title = self.get_element(UserDashboardLactor.all_noti_title, doc).text
        assert notify_title == "All Notifications"

    def check_credit(self, cmdopt):
        doc='查看信控值'
        self.wait_elevisble(UserDashboardLactor.credit_limit_ele, doc)
        credit_limit = self.get_element(UserDashboardLactor.credit_limit_ele, doc).text
        print("信控值" + credit_limit)
        available_credit = self.get_element(UserDashboardLactor.available_credit_ele, doc).text
        print("可用信控值" + available_credit)
        available_bal = self.get_element(UserDashboardLactor.available_bal_ele, doc).text
        print("可用余额" + available_bal)
        available_scale = self.get_element(UserDashboardLactor.available_scale_ele, doc).text
        print(available_scale)

        assert available_credit !=''
        assert credit_limit !=''
        assert available_scale !=''
        assert available_bal !=''
    ##企业客户
    def dashboard_addfunds(self):
        doc = '在dashboard点击addfunds'
        time.sleep(2)
        self.wait_elevisble(UserDashboardLactor.add_funds_btn, doc)
        self.click_element(UserDashboardLactor.add_funds_btn, doc)
        self.wait_elevisble(UserDashboardLactor.add_funds_title, doc)
        title = self.get_element(UserDashboardLactor.add_funds_title, doc).text
        print(title)
        assert title == "Add Funds"

    ##个人客户
    def check_personal_credit(self, cmdopt):
        doc='查看个人客户账户余额'
        self.wait_elevisble(UserDashboardLactor.credit_limit_ele, doc)
        available_bal1 = self.get_element(UserDashboardLactor.credit_limit_ele, doc).text
        print("个人客户账户余额" + available_bal1)
        assert available_bal1 != ''
        self.wait_elevisble(UserDashboardLactor.person_addfund_ele, doc)
        self.click_element(UserDashboardLactor.person_addfund_ele, doc)
        self.wait_elevisble(UserDashboardLactor.add_funds_title, doc)
        title = self.get_element(UserDashboardLactor.add_funds_title, doc).text
        print(title)
        assert title == "Add Funds"

    def dashboard_welcome_message(self):
        # 使用datetime模块获取当前日期和星期几
        current_date = datetime.now()
        day_of_week = current_date.strftime('%A')  # 获取当前星期几
        month = current_date.strftime('%B')  # 获取当前月份
        day = current_date.day  # 获取当前日期
        year = current_date.year  # 获取当前年份

        # 将日期和星期几格式化成指定的格式
        formatted_date = f"{day_of_week},{month} {day} {year},"
        # 打印格式化后的日期
        print(formatted_date)
        doc ="检查dashboard的welcome messge"
        self.wait_elevisble(UserDashboardLactor.welcome_info_ele, doc)
        welcome_info = self.get_element(UserDashboardLactor.welcome_info_ele, doc).text
        print("welcome_info is " + welcome_info)
        welcomeinfo = formatted_date +"Welcome back to Whale cloud user center."
        print(welcomeinfo)

    def dashboard_recent_used_prod(self):
        doc = "检查dashboard的recent used product"
        self.wait_elevisble(UserDashboardLactor.recent_usedprod_title, doc)
        recent_usedProd_titile = self.get_element(UserDashboardLactor.recent_usedprod_title, doc).text
        print(recent_usedProd_titile)
        assert recent_usedProd_titile == "Recently Used Product"
        used_prod = []
        eles = self.get_elements(UserDashboardLactor.used_prod_ele, doc)
        # 获取订单prod数组
        for ele in eles:
            used_prod.append(ele.text)
        print(used_prod)
        ##检查消息最多8条
        if 0 <= len(used_prod) <= 8:
            print("dashboard展示用过的产品数量在 0 到 8 之间")
            check_info = 'YES'
        else:
            check_info = 'NO'
            print("dashboard展示用过的产品数量不在 0 到 8 之间")
        assert check_info == "YES"

    # 待修改
    def dashboard_in_used_prod(self):
        time.sleep(3)
        doc="dashboard查看current in use product,和region id"
        self.wait_elevisble(UserDashboardLactor.in_use_ele, doc)
        in_use_title = self.get_element(UserDashboardLactor.in_use_ele, doc).text
        assert in_use_title == "My Resources"
        time.sleep(2)
        # self.wait_elevisble(UserDashboardLactor.product_ele, doc)
        # self.move_mouse_to_element(UserDashboardLactor.product_ele)
        # self.wait_elevisble(UserDashboardLactor.region_id_ele, doc)
        # region_id = self.get_element(UserDashboardLactor.region_id_ele, doc).text
        # print("鼠标悬浮于产品上方可展示regionid,值为" + region_id)

    def dashboad_view_first_info(self):
        doc="dashboard查看lastest info详情"
        #self.move_mouse_to_element(UserDashboardLactor.lastestInfo_title)
        self.wait_elevisble(UserDashboardLactor.first_info_ele, doc)
        self.click_element(UserDashboardLactor.first_info_ele, doc)
        self.wait_elevisble(UserDashboardLactor.info_title_ele, doc)
        first_info_title = self.get_element(UserDashboardLactor.info_title_ele, doc).text
        print ("my latest info titile is:" +first_info_title)

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 查看Frozen Balance（IDR-跟随项目币种）
    def check_dashboard_frozen_balance(self):
        time.sleep(3)
        doc = "查看Frozen Balance（IDR-跟随项目币种）"
        self.wait_elevisble(UserDashboardLactor.frozen_balance_title, doc)
        self.check_attribute(self.get_element(UserDashboardLactor.frozen_balance_title, doc).text, 'Frozen Balance (IDR)')
        self.check_attribute(self.get_element(UserDashboardLactor.frozen_balance_num, doc).text, '1,008,325,159.35')

    # 查看新增Currently in Use
    def dashboard_add_currently_use(self):
        time.sleep(3)
        doc = "查看新增Currently in Use"
        self.scroll_bar_operation(UserDashboardLactor.currently_use_title)
        time.sleep(1)
        self.check_attribute(self.get_element(UserDashboardLactor.currently_use_title, doc).text,'Currently in Use')
        self.check_attribute(self.get_element(UserDashboardLactor.currently_ecs_title, doc).text, 'Elastic Compute Service')
        self.check_attribute(self.get_element(UserDashboardLactor.currently_rds_mysql_title, doc).text, 'RDS for MySQL')

    # 查看Increase credit limit入口
    def dashboard_enter_increase_credit_limit(self):
        time.sleep(3)
        doc = "查看Increase credit limit入口"
        # 点击Increase credit limit按钮打开弹框
        self.move_mouse_to_click_element(UserDashboardLactor.increase_credit_limit_btn, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(UserDashboardLactor.increase_credit_limit_box_title, doc).text, 'Increase Credit Limit')
        self.check_attribute(self.get_element(UserDashboardLactor.total_credit_limit_title, doc).text,'Total Credit Limit (IDR)')
        self.check_attribute(self.get_element(UserDashboardLactor.add_credit_limit_box_title, doc).text, 'Add Credit Limit to')

    # 查看Increase credit limit页面
    def dashboard_check_increase_credit_limit(self):
        time.sleep(3)
        doc = "查看Increase credit limit页面"
        # 获取dashboard页面的现有可用余额金额
        total_credit = self.get_element(UserDashboardLactor.dashboard_total_credit_limit, doc).text
        # 点击Increase credit limit按钮打开弹框
        self.move_mouse_to_click_element(UserDashboardLactor.increase_credit_limit_btn, doc)
        time.sleep(1)
        self.save_screen(doc)
        self.check_attribute(self.get_element(UserDashboardLactor.dashboard_box_total_credit_limit, doc).text, total_credit)
        self.check_attribute(self.get_attribute(UserDashboardLactor.dashboard_box_add_credit_limit, 'placeholder', doc),'3,244,444,446~16,222,222,225')

    # 验证资源数字可以跳转到my resource
    def dashboard_check_jump_myresource(self):
        time.sleep(3)
        doc = "验证资源数字可以跳转到my resource"
        self.wait_elevisble(UserDashboardLactor.dashboard_resources_expire_title, doc)
        dashboard_resources_expire = self.get_element(UserDashboardLactor.dashboard_resources_expire_btn, doc).text
        # 点击资源失效提醒热区按钮进入my resources页面
        self.move_mouse_to_click_element(UserDashboardLactor.dashboard_resources_expire_btn, doc)
        time.sleep(4)
        self.wait_elevisble(UserDashboardLactor.my_resources_page, doc)
        if int(dashboard_resources_expire) == 0:
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_no_data_dsc, doc).text, 'No Data')
        else:
            # 检测当前过期时间自动选择“当前时间+7天”的数据是否一致
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_expire_data, doc).text, 'Total ' + dashboard_resources_expire + ' items')
        # 点击首页Dashboard栏标题按钮返回Dashboard页面
        self.move_mouse_to_click_element(UserDashboardLactor.dashboard_title_btn, doc)
        time.sleep(4)
        self.wait_elevisble(UserDashboardLactor.dashboard_critical_title, doc)
        dashboard_critical = self.get_element(UserDashboardLactor.dashboard_critical_btn, doc).text
        self.move_mouse_to_click_element(UserDashboardLactor.dashboard_critical_btn, doc)
        time.sleep(4)
        self.wait_elevisble(UserDashboardLactor.my_resources_page, doc)
        if int(dashboard_critical) == 0:
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_no_data_dsc, doc).text, 'No Data')
        else:
            # 检测Critical部分的数据是否一致
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_critical_data, doc).text,'Total ' + dashboard_critical + ' items')
        # 点击首页Dashboard栏标题按钮返回Dashboard页面
        self.move_mouse_to_click_element(UserDashboardLactor.dashboard_title_btn, doc)
        time.sleep(4)
        self.wait_elevisble(UserDashboardLactor.dashboard_warning_title, doc)
        dashboard_warning = self.get_element(UserDashboardLactor.dashboard_warning_btn, doc).text
        self.move_mouse_to_click_element(UserDashboardLactor.dashboard_warning_btn, doc)
        time.sleep(4)
        self.wait_elevisble(UserDashboardLactor.my_resources_page, doc)
        if int(dashboard_warning) == 0:
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_no_data_dsc, doc).text, 'No Data')
        else:
            # 检测Critical部分的数据是否一致
            self.check_attribute(self.get_element(UserDashboardLactor.my_resource_warning_start_data, doc).text,'Total ' + dashboard_warning + ' items')





















